package org.spring.blog.interceptor;

import com.google.gson.Gson;
import org.spring.blog.response.ResponseResult;
import org.spring.blog.service.IUserService;
import org.spring.blog.utils.CookieUtil;
import org.spring.blog.utils.LRConstants;
import org.spring.blog.utils.RedisUtil;
import org.spring.blog.utils.TextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@Component
public class ApiInterceptor extends HandlerInterceptorAdapter {

    @Autowired
    Gson gson;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {


        // 指定允许其他域名访问
//        response.setHeader("Access-Control-Allow-Origin", "*"); // 允许所有
//        response.setHeader("Access-Control-Allow-Origin", "http://localhost:3000"); // 允许白名单IP
//        // 响应类型
//        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
//        // 预检请求的结果缓存60分钟
//        response.setHeader("Access-Control-Max-Age", "3600");
//        // 响应头设置
//        response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
//
//        response.setHeader("Access-Control-Allow-Credentials", "true");

        String tokenKey = CookieUtil.getCookieValue(request, IUserService.constant.cookie_token_key);

        // check data
        if (handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod) handler;


            CheckToolFrequentCommit methodAnnotation = handlerMethod.getMethodAnnotation(CheckToolFrequentCommit.class);

            if (methodAnnotation != null) {
                if (TextUtil.notEmpty(tokenKey)) {

                    String methName = handlerMethod.getMethod().getName();

                    String commitFrequentFlag = (String) redisUtil.get(LRConstants.redis_user_commit_flag + methName);

                    if (TextUtil.notEmpty(commitFrequentFlag)) {

                        // complete return data
                        response.setCharacterEncoding("utf-8");
                        response.setContentType("application/json");

                        ResponseResult result = ResponseResult.failed("提交太频繁");

                        PrintWriter writer = response.getWriter();
                        writer.write(gson.toJson(result));
                        writer.flush();

                        return false;
                    } else {
                        redisUtil.set(LRConstants.redis_user_commit_flag + methName, tokenKey, 30);
                    }
                }
            }
        }

        return true;
    }
}